package com.example.webservice;





import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;




import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.example.webservice.dao.Factory.FactoryDAO;
import com.example.webservice.dao.Interface.ClienteDAO;
import com.google.gson.Gson;

@Path("/user")
public class UserResource {
	List<Cliente> clientes = null;
	static final int TAMANHO_BUFFER = 1024; // 1kb

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/user")
	public List<Cliente> getJSON() {	
		
	ClienteDAO clienteDao = FactoryDAO.createClienteDao();	
	clientes = clienteDao.getClienteByPromotor(471);	
		
	
		
		Gson gson = new Gson();

		String saida = gson.toJson(clientes);
		System.out.println(saida);
		
			Map<String, String> entries = new HashMap<String, String>();
			entries.put("firstContent", "1.txt");

			entries.put(saida, "2.txt");
			entries.put("thirdContent", "3.txt");
	
	      FileOutputStream fos = null;
          ZipOutputStream zos = null;
	      try {	          
	          
	          fos = new FileOutputStream("c:\\Temp\\output.zip");
	          zos = new ZipOutputStream(fos);
	          for (Map.Entry<String, String> mapEntry : entries.entrySet()) {
	              ZipEntry entry = new ZipEntry(mapEntry.getValue()); // create a new zip file entry with name, e.g. "1.txt"
	              entry.setMethod(ZipEntry.DEFLATED); // set the compression method
	              zos.putNextEntry(entry); // add the ZipEntry to the ZipOutputStream
	              zos.write(mapEntry.getKey().getBytes()); // write the ZipEntry content
	          }
	//         System.out.println(toByteArray(new File("c:\\Temp\\output.zip"))); 
	         return clientes;    
	      } catch (Exception e) {				
				e.printStackTrace();
				return null;
			} finally {
	    	        try {
						zos.close();
					} catch (IOException e) {
			
						e.printStackTrace();
					}    	    
	    	}    	
	}
	

	
	
	/*public byte[] toByteArray(File file) throws Exception  { 
		 
        return org.apache.commons.io.FileUtils.readFileToByteArray(new File); 
        

	}*/



@GET //metodo web 
@Produces("application/zip") //tipo de retorno zip/xml/json
@Path("/getFile") //caminho
public Response getFile(@Context HttpServletResponse response){ 
	ClienteDAO clienteDao = FactoryDAO.createClienteDao();	//padrao dao + mvc
	clientes = clienteDao.getClienteByPromotor(471);		//padrao dao + mvc
	Gson gson = new Gson();
	String saida = gson.toJson(clientes);  //transforma a lista de cliente em json
	
		response.setHeader("Content-Disposition", "attachment; filename=\"content.zip\"");   //retorno do responser
		try {
			ZipOutputStream out = new ZipOutputStream(response.getOutputStream()); //stream zip
			zip(saida,out,"teste.txt");	 //inclui string no zip
			out.flush();
			out.close();
		} catch (IOException e) {		
			e.printStackTrace();
		}		

	return Response.ok().build(); ///retorna response
}

protected void zip(String conteudo, ZipOutputStream out,String fileName ) throws IOException{
	ZipEntry entry = new ZipEntry(fileName);
	out.putNextEntry(entry);
    out.write(conteudo.getBytes()); 

}





}